/*
* Created by: yyf
* Description: 母基金管理--投资中母基金 新建
*/
<template>
    <div>
        <div class="right ca-p-t-50">
            <el-row>
                <el-col :span="24" class="ca-align-right">
                    <div class="ca-head-fixed">
                        <el-button type="primary" @click="fundAdd()">保存</el-button>
                        <el-button @click="exit()">退出</el-button>
                    </div>
                </el-col>
            </el-row>
            <div class="ca-box ca-p-20">
                <el-form label-width="100px" ref="fundForm" :rules="rules" :model="fund">
                    <el-row>
                        <el-col :span="11">
                            <el-form-item label="基金全称" prop="fundFullname">
                                <el-input v-model="fund.fundFullname"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="11" :offset="1">
                            <el-form-item label="基金简称"  prop="fundName">
                                <el-input v-model="fund.fundName"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
            <csDetailEdit
                ref="detailEdit"
                :isAdd="true"
                :fund="fund"
                :fundMother="fundMother"
                :region="region"
                @changeRegion="changeRegion">
            </csDetailEdit>
        </div>
    </div>
</template>

<script type="text/ecmascript-6">
    import {api} from './api';
    import {STATUS_HTTP_SUCCESS, apiHttp} from '__service/csHttp';
    import { addRemind } from '__service/utils';
    import { isNoAuth } from '__service/utils';
    export default {
        name: 'invested-FOF-add',
        data() {
            return {
                itemId: '',
                initView: {
                    fundCategory: [{id: '', name: '请选择'}, ...this.$store.state.user.dictionaryMap.fundCategory],// 基金类别
                    fundType: [{id: '', name: '请选择'}, ...this.$store.state.user.dictionaryMap.fundType],// 基金类型
                    reference: [{id: '', name: '请选择'}, ...this.$store.state.user.dictionaryMap.whether], // 是否备案
                },
                startTime: '',
                fundType: '',
                rules: {
                    fundFullname: [
                        { required: true, message: '请输入基金全称', trigger: 'blur' },
                    ],
                    fundName: [
                        { required: true, message: '请输入基金简称', trigger: 'blur' },
                    ],
                },
                fund: { // 母基金基金基础信息
                    establishTime: '',   // 拟设立日期
                    exitPeriod: '',      // 退出期
                    exitTime: '',        // 退出时间
                    fundCategory: '',    // 基金类别
                    fundFullname: '',    // 基金全称
                    fundName: '',        // 基金简称
                    fundScale: null,     // 基金规模
                    fundState: 4,     // 设立状态
                    fundType: '',         // 基金类型
                    id: '',               // 基金主id
                    investmentPeriod: '', // 投资期
                    treeType: '2'          // 1：子基金；2：母基金 对应字典表：fundTree
                },
                fundMother: { // 母基金详情
                    backups: '',          // 是否备案
                    brief: '',            // 基金简介 ,
                    chairman: '',         // 董事长
                    custodian: '',        // 基金托管人
                    fundId: '',           // 基金基础表id
                    generalManager: '',   // 总经理
                    id: '',               // 主id
                    manageFee: '',        // 基金管理费
                    managementCompany: '',// 管理公司
                    manager: '',          // 基金管理人
                    managerName: '',      // 基金经理
                    supervisor: ''        // 监事会主席
                },
                region: [],
                isAuthListAdd: isNoAuth('投资中母基金-列表页-新建'),
            }
        },
        beforeRouteEnter(to, from, next) {
            next( vm => {
                if (!vm.isAuthListAdd) {
                    vm.$message({
                        message: '没有此页面的操作权限',
                        type: 'error',
                        center: true
                    });
                    vm.$router.replace({name: 'personal-center', params: {tabIndex: '0', openWindow: false}})
                }
            })
        },
        mounted() {
        },
        methods: {
            // 保存
            fundAdd() {
                this.$refs.fundForm.validate(async (valid) => {
                    if (valid) {
                        let isSave = this.$refs.detailEdit.validateAdd();  // 验证通过，可以保存
                        if (isSave) {
                            let {resCode, data} = await apiHttp(api.ADD_FUND_MOTHER, {
                                fund: this.fund,
                                fundMother: this.fundMother,
                                region: this.region
                            });
                            if (resCode === STATUS_HTTP_SUCCESS) {
                                this.itemId = data.id;
                                this.$message({
                                    type: 'success',
                                    message: '添加成功！',
                                    center: true
                                })
                                this.addRedDot();
                                this.$router.push({name: 'invested-FOF-list', params: {openWindow: false}})
                            }
                        }
                    }
                })
            },
            // 退出新增页面
            exit() {
                this.$confirm('是否退出', '提示', {
                    cancelButtonText: '否',
                    confirmButtonText: '是',
                    type: 'warning'
                }).then(() => {
                    this.close();
                })
            },
            // 返回列表页
            close() {
                this.$router.push({name: 'invested-FOF-list', params: {openWindow: false}})
            },
            // 改变注册地点
            changeRegion(val) {
                this.region = [];
                this.region.push({
                    businessId: this.itemId,
                    businessType: val.businessType,
                    city: val.city,               // 市
                    district: val.district,       // 区
                    id: '',                       // id
                    province: val.province,       // 省
                    registrationDetailed: val.registrationDetailed,  // 详细地址
                })
            },
            // 添加小红点
            async addRedDot() {
                await addRemind({
                    dataId: this.itemId, // 业务id
                    dataTab: 0, // tab页顺序 head 也为0 新建为'-1'
                    dataType: 3, // 1拟投资项目,2已投资项目,3母基金,4拟设立基金,5已设立基金,6客户,7潜在项目
                });
                // 根据第三方组件来调用 父组件中定义的方法 重新获取小红点
                this.$root.Dot.$emit('getDotList');
                // 添加小红点 end
            },
        },
        components: {
            'csDetailEdit': require('./detail/edit'), // 详情编辑
        }
    }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
    .ca-w-100 {
        width: 100%;
    }
</style>
